﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

using System.Drawing.Printing;
namespace LOGIN
{
    public partial class MonthlyendingReports : Form
    {
        public MonthlyendingReports()
        {
            InitializeComponent();
        
        }

        private void MonthlyendingReports_Load(object sender, EventArgs e)
        {
            this.Tip.Visible = false;
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                "Server=(local);Database=MyHospital;Integrated Security=sspi";
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT DISTINCT CONVERT(varchar(10),datepart(yy,OrderDate))+'年'+ CONVERT(varchar(10),datepart(mm,OrderDate))+'月' AS date FROM orders;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                        
            sqlDataAdapter.SelectCommand = sqlCommand;                                               
            DataTable DateTable = new DataTable();
            sqlConnection.Open();                                                                     
            sqlDataAdapter.Fill(DateTable);
            this.cmb_date.DataSource = DateTable;
            this.cmb_date.DisplayMember = "date";
            PreviousBalance.Visible = false;
            MonthBreak.Visible = false;
            Different.Visible = false;
           
        }

        private void OK_Click(object sender, EventArgs e)
        {
            PreviousBalance.Visible = true;
            MonthBreak.Visible = true;
            Different.Visible = true;
            PreviousBalance.Text =Convert.ToString(245274);
            MonthBreak.Text = Convert.ToString(1478);
            Different.Text = Convert.ToString(325);
            time.Text = cmb_date.Text.ToString();
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                "Server=(local);Database=MyHospital;Integrated Security=sspi";
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText =                                                                  
                "SELECT Lyl*IndPrice AS Useprice FROM medicineBuy where CONVERT(varchar(10),datepart(yy,Lydate))+'年'+ CONVERT(varchar(10),datepart(mm,Lydate))+'月'=CONVERT(VARCHAR(30),@date);"                                                         
                + "SELECT Num*IndPrice AS TotalInprice FROM orders where CONVERT(varchar(10),datepart(yy,OrderDate))+'年'+ CONVERT(varchar(10),datepart(mm,OrderDate))+'月'=CONVERT(VARCHAR(30),@date) AND State=1;"
                + "SELECT Num*IndPrice AS cancal FROM orders where CONVERT(varchar(10),datepart(yy,OrderDate))+'年'+ CONVERT(varchar(10),datepart(mm,OrderDate))+'月'=CONVERT(VARCHAR(30),@date) AND State=1 AND cancel=1;";
            sqlCommand.Parameters.AddWithValue("@date", this.cmb_date.Text.ToString());
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                        
            sqlDataAdapter.SelectCommand = sqlCommand;                                                     
            DataSet dataSet = new DataSet();                                                        
            sqlConnection.Open();                                                                         
            sqlDataAdapter.Fill(dataSet);                                                               
            sqlConnection.Close();                                                                       
            DataTable UsepriceTable = dataSet.Tables[0];                                                  
            DataTable TotalInpriceTable = dataSet.Tables[1];                                                       
            DataTable cancalTable = dataSet.Tables[2];
            this.Monthbuy.Text = UsepriceTable.Compute("SUM(Useprice)", "").ToString();
            this.MonthInput.Text = TotalInpriceTable.Compute("SUM(TotalInprice)", "").ToString();
            this.MonthCancel.Text = cancalTable.Compute("SUM(cancal)", "").ToString();
            int i = 0;
            float m = 0;
            if (!float.TryParse(this.Monthbuy.Text, out m))
            {
                this.Monthbuy.Text = "0";
            }
            if (!int.TryParse(this.MonthInput.Text, out i))
            {
                this.MonthInput.Text = "0";
            }
            if (!int.TryParse(this.MonthCancel.Text, out i))
            {
                this.MonthCancel.Text = "0";
            }
            int a = Int32.Parse(this.PreviousBalance.Text.Trim());
            float b = float.Parse(this.Monthbuy.Text);
            int c = Convert.ToInt32(this.MonthInput.Text);
            int d = Convert.ToInt32(this.MonthCancel.Text);
            int f = Convert.ToInt32(this.MonthBreak.Text);
            int g = Convert.ToInt32(this.Different.Text);
            this.CurrentBalance.Text = Convert.ToString(a + c + g - f  -b - d);

            this.Tip.Visible = true;
             
      

        }

        private Bitmap memImage = null;

        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern bool BitBlt(
        IntPtr hdcDest,
        int nXDest,
        int nYDest, 
        int nWidth, 
        int nHeight,
        IntPtr hdcSrc, 
        int nXSrc, 
        int nYSrc,  
        System.Int32 dwRop 
        );

        private void print_Click(object sender, EventArgs e)
        {
            
            Graphics graphic = panel1.CreateGraphics();
            Size s = panel1.Size;
            memImage = new Bitmap(s.Width, s.Height, graphic);
            Graphics memGraphic = Graphics.FromImage(memImage);
            IntPtr dc1 = graphic.GetHdc();
            IntPtr dc2 = memGraphic.GetHdc();

            BitBlt(dc2, 0, 0, panel1.ClientRectangle.Width, panel1.ClientRectangle.Height,
                  dc1, 0, 0, 13369376);
           

           
            Image print_image = (Image)memImage.Clone();
            graphic.ReleaseHdc(dc1);
            memGraphic.ReleaseHdc(dc2);
            graphic.Dispose();
            memGraphic.Dispose();
   

            PrintPreviewDialog dlg = new PrintPreviewDialog();
            dlg.Width = 2200;
            dlg.Height = 2000;
            dlg.Document = printDocument1;

            if (dlg.ShowDialog() == DialogResult.OK)
                printDocument1.Print();    
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(memImage, 100, 100);
        }

        private void crystalReportViewer1_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       

     

        
        
       

     
    }
}
